<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set timeout period on a stream</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-set-timeout" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_set_timeout</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_set_timeout</span> &mdash; <span class="dc-title">Set timeout period on a stream</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-set-timeout-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_set_timeout</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$seconds</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$microseconds</code><span class="initializer"> = 0</span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Sets the timeout value on <code class="parameter">stream</code>,
   expressed in the sum of <code class="parameter">seconds</code> and
   <code class="parameter">microseconds</code>. 
  </p>
  <p class="para">
   When the stream times out, the &#039;timed_out&#039; key of the array returned by
   <span class="function"><a href="stream_get_meta_data.html" class="function">stream_get_meta_data()</a></span> is set to <strong><code>TRUE</code></strong>, although no
   error/warning is generated.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-set-timeout-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">
       The target stream.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">seconds</code></dt>

     <dd>

      <p class="para">
       The seconds part of the timeout to be set.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">microseconds</code></dt>

     <dd>

      <p class="para">
       The microseconds part of the timeout to be set.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-set-timeout-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.stream-set-timeout-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.0</td>
       <td>
        As of PHP 4.3, this function can (potentially) work on any kind of
        stream.  In PHP 4.3, socket based streams are still the only kind
        supported in the PHP core, although streams from other extensions
        may support this function.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-set-timeout-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4834">
    <p><strong>Example #1 <span class="function"><strong>stream_set_timeout()</strong></span> example</strong></p>
     <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"www.example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;open\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"GET&nbsp;/&nbsp;HTTP/1.0\r\n\r\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stream_set_timeout</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'timed_out'</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Connection&nbsp;timed&nbsp;out!'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.stream-set-timeout-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function doesn&#039;t work with advanced operations like
    <span class="function"><a href="stream_socket_recvfrom.html" class="function">stream_socket_recvfrom()</a></span>, use
    <span class="function"><a href="stream_select.html" class="function">stream_select()</a></span> with timeout parameter instead.
   </p>
  </p></blockquote>
  <p class="para">
   This function was previously called as
   <span class="function"><strong>set_socket_timeout()</strong></span> and later
   <span class="function"><a href="socket_set_timeout.html" class="function">socket_set_timeout()</a></span> but this usage is deprecated.
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-set-timeout-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开一个网络连接或者一个Unix套接字连接</span></li>
   <li class="member"><span class="function"><a href="fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
  </ul>
 </div>

</div></div></div></body></html>